package com.sky.controller.admin;

import com.aliyuncs.http.HttpResponse;
import com.sky.result.Result;
import com.sky.service.ReportSrevice;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/admin/report")
@Slf4j
public class ReportController {

    @Autowired
    private ReportSrevice reportSrevice;

    /**
     * 数据统计
     */
    @GetMapping("turnoverStatistics")
    public Result<TurnoverReportVO> getBusinessData(
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate end) {
        log.info("查询订单统计数据时间{},{}",begin,end);
        return  Result.success(reportSrevice.getTurnoverData(begin,end)) ;
    }

    /**
     * 统计人数
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("userStatistics")
    public Result<UserReportVO> getOrderStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate end
    ) {
        log.info("查询用户统计数据时间{},{}",begin,end);
        return Result.success(reportSrevice.getOrderStatistics(begin,end));

    }
    /**
     * 统计订单
     */
    @GetMapping("ordersStatistics")
    public Result<OrderReportVO> getOrdersStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate end
    ){
        log.info("查询订单统计数据时间{},{}",begin,end);
        return Result.success(reportSrevice.getOrdersStatistics(begin,end));
    }

    /**
     * 销量前十名
     */
    @GetMapping("/top10")
    public Result<SalesTop10ReportVO> getTopTen(
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate end
    ){
        log.info("查询销量前十数据时间{},{}",begin,end);
        return Result.success(reportSrevice.getTopTen(begin,end));
    }

    /**
     * 报表输出
     * @param response
     * @return
     */
    @GetMapping("/export")
    public Result getReport(HttpServletResponse response) {
        log.info("输出保镖统计");
        reportSrevice.getReport(response);
        return Result.success();
    }


}
